/*
 * data_type.c
 *
 *  Created on: 2019年5月7日
 *      Author: Administrator
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "../include/data_type.h"

/**
 * C语言数据类型：基本类型、enum类型、void类型、派生类型、指针类型
 * enum类型是被当做int类型来处理的。在一些特殊的情况下，比如枚举类型是连续的，可以实现遍历
 *
 * C99 引入布尔类型。通过宏定义实现
 * #include <stdbool.h>
 * bool flag = false;
 */
void basic_type(void) {

	// sizeof() 返回变量存储大小运算符
	// 显示数据类型的存储字节大小，字节为单位
	printf("char 存储大小：%d \n", sizeof(char));
	printf("short 存储大小：%d \n", sizeof(short));
	printf("int 存储大小：%d \n", sizeof(int));
	printf("long 存储大小：%d \n", sizeof(long));
	printf("float 存储大小：%d \n", sizeof(float));
	printf("double 存储大小：%d \n", sizeof(double));

	// 基本类型
	int left_data = 13;
	int right_data = 1;
	float result;

	printf("result 存储大小：%d \n", sizeof(result));
//	printf("result: %f \n", result);
	result = left_data + right_data;
	printf("result: %f \n", result);

	// 枚举enum类型
	// 声明
	enum Season {
		SPRING = 1, SUMMER, AUTUM, WINTER
	};
	enum Season season = SUMMER;
	printf("season:%d \n", season);

	// 遍历
	// 如果enum类型是连续的，可以for遍历
	for (season = SPRING; season <= WINTER; season++) {
//		printf("season:%d \n", season);
		// switch
		switch (season) {
		case SPRING:
			printf("season:%d 春天！ \n", season);
			break;
		case SUMMER:
			printf("season:%d 夏天！ \n", season);
			break;
		case AUTUM:
			printf("season:%d 秋天！ \n", season);
			break;
		case WINTER:
			printf("season:%d 冬天！ \n", season);
			break;
		default:
			printf("无效！");
		}
	}

	bool flag = false;
	if (season != flag) {
		printf("season:%d \n", season);
	}

}

/**
 * struct
 * C语言结构体是用户自定义的可用的数据类型，它允许存储不同类型的数据项
 */
void struct_type() {

	// struct type_name
	// 初始化结构体的方式：

	struct Date today;
	today.day = 15;
	today.month = 5;
	today.year = 2019;

	// 定义时赋值
	struct Date today2 = { .day = 15, .month = 5, .year = 2019 };

	struct Date tomorrow = { 2019, 5, 16 };

	struct Date * yesterday = (struct Date *) malloc(sizeof(struct Date));
	yesterday->year = today.year;
	yesterday->month = today.month;
	yesterday->day = today.day - 1;

	print_date(yesterday);

	//	printf("今天是：%d年%d月%d日 \n", today.year, today.month, today.day);
	print_date(&today);

	printf("tomorrow 存储大小：%d \n", sizeof(tomorrow));
	printf("struct Date 存储大小：%d \n", sizeof(struct Date));
	printf("struct Date 存储大小：%d \n", sizeof(today2));

	// typedef 形式
	Person *person = (Person *)calloc(1, sizeof(Person));
	person->id = 1;
	strcpy(person->name, "Ge Yang");
	person->age = 27;
	strcpy(person->address, "Xin jiang urimqi");

	print_person(*person);
}

void print_date(void * ptr) {
	struct Date * date = (struct Date *)ptr;
	printf("Year: %d \n", date->year);
	printf("Month: %d \n", date->month);
	printf("Day: %d \n", date->day);
}

void print_person(Person person) {

	printf("Person %d: {name: %s, age:%d, address:%s}", person.id, person.name,
			person.age, person.address);
}

void int2char() {
	// itoa :把int类型转为char类型
	int test = 43;

	char result[50];
	itoa(test, result, 10);

	printf("itoa: %s", result);

}

void char2int() {

	// atoi: 把字符串转换为整型
	int n;
	char str[] = "12345.67";
	n = atoi(str);
	printf("string = %s integer = %d\n", str, n);
}
